﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace Fitment.User
{
    public partial class testroomMatInfo : System.Web.UI.Page
    {
        //新建数据库操作对象
        DS data = new DS();

        //读取配置文件连接信息
        string strConnection = ConfigurationManager.ConnectionStrings["connection"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Session["userName"].ToString();   //用于获取用户登录身份
            this.user.Text = userName;
            //如果Session为空,则转到登陆页.
            if (Session["User_ID"] == null)
            {
                Response.Redirect("../logon.aspx");
            }

            //如果Session为空,则转到登陆页.
            if (Session["House_ID"] == null)
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "", " <SCRIPT>if(confirm('您还没有输入要装修的房子信息,请选择房子进行编辑或者重新输入房子信息！')) window.location.href = 'myHouse.aspx';</SCRIPT>");
            }
            else
            {
                //房间数
                int num = 0;
                int DoorOrdinal = 0;
                int WindowOrdinal = 0;
                int OtherOrdinal = 0;
                int OtherOrdinalArea = 0;

                //获取输入数据
                int houseId = Convert.ToInt32(Session["House_ID"].ToString());
                //得到登录用户的各个房间信息,动态生成控件
                string GetRoomInfo = "SELECT * FROM RoomInfo WHERE House_ID = '" + houseId + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(GetRoomInfo, connection);
                    connection.Open();
                    SqlDataReader Reader = command.ExecuteReader();
                    while (Reader.Read())
                    {
                        num++;
                        Panel P2 = new Panel();
                        P2.ID = "P" + num.ToString();
                        Panel1.Controls.Add(P2);
                        Panel P3 = new Panel();
                        P2.Controls.Add(P3);
                        Label RoomIDText = new Label();
                        RoomIDText.Text = "房间编号:";
                        Label RoomID = new Label();
                        RoomID.ID = "RoomID_" + num.ToString();
                        RoomID.Text = Reader[0].ToString();
                        Label RoomType = new Label();
                        RoomType.ID = "RoomType_" + num.ToString();
                        RoomType.Text = Reader[2].ToString();

                        P3.Controls.Add(RoomIDText);
                        P3.Controls.Add(RoomID);
                        P3.Controls.Add(RoomType);

                        Panel P4 = new Panel();
                        P2.Controls.Add(P4);
                        Label LengthText = new Label();
                        LengthText.Text = "长";
                        TextBox Length = new TextBox();
                        Length.Text = Reader[4].ToString();
                        Length.ID = "Length_" + num.ToString();

                        Label WidthText = new Label();
                        WidthText.Text = "宽";
                        TextBox Width = new TextBox();
                        Width.Text = Reader[5].ToString();
                        Width.ID = "Width_" + num.ToString();

                        Label HeightText = new Label();
                        HeightText.Text = "高";
                        TextBox Height = new TextBox();
                        Height.Text = Reader[3].ToString();
                        Height.ID = "Height_" + num.ToString();

                        P4.Controls.Add(LengthText);
                        P4.Controls.Add(Length);
                        P4.Controls.Add(WidthText);
                        P4.Controls.Add(Width);
                        P4.Controls.Add(HeightText);
                        P4.Controls.Add(Height);
                    }
                    Reader.Close();
                    connection.Close();
                }

                for (int j = 1; j <= num; j++)
                {
                    DoorOrdinal = 0;
                    WindowOrdinal = 0;
                    OtherOrdinal = 0;
                    OtherOrdinalArea = 0;
                    //生成属于该房间的门窗
                    string PID = "P" + j.ToString();
                    Panel PP2 = (Panel)FindControl(PID);
                    string RID = "RoomID_" + j.ToString();
                    Label R = (Label)FindControl(RID);
                    Panel P6 = new Panel();
                    PP2.Controls.Add(P6);
                    string DoorQuery = " SELECT * FROM DoorInfo WHERE Room_ID='" + R.Text + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(DoorQuery, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        while (Reader.Read())
                        {
                            DoorOrdinal++;
                            Label DoorNum = new Label();
                            DoorNum.Text = "门" + DoorOrdinal.ToString() + ":";

                            Label Length = new Label();
                            Length.Text = "长";
                            TextBox LengthInt = new TextBox();
                            LengthInt.Text = Reader[3].ToString();
                            LengthInt.ID = "DoorLength_" + j.ToString() + DoorOrdinal.ToString();
                            Label Height = new Label();
                            Height.Text = "高";
                            TextBox HeightInt = new TextBox();
                            HeightInt.Text = Reader[2].ToString();
                            HeightInt.ID = "DoorHeight_" + j.ToString() + DoorOrdinal.ToString();
                            P6.Controls.Add(DoorNum);
                            P6.Controls.Add(Length);
                            P6.Controls.Add(LengthInt);
                            P6.Controls.Add(Height);
                            P6.Controls.Add(HeightInt);
                        }
                        Reader.Close();
                        connection.Close();
                    }

                    Panel P7 = new Panel();
                    PP2.Controls.Add(P7);
                    string WindowQuery = " SELECT * FROM WindowInfo WHERE Room_ID='" + R.Text + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(WindowQuery, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        while (Reader.Read())
                        {
                            WindowOrdinal++;
                            Label WindowNum = new Label();
                            WindowNum.Text = "窗" + WindowOrdinal.ToString() + ":";

                            Label Length = new Label();
                            Length.Text = "长";
                            TextBox LengthInt = new TextBox();
                            LengthInt.Text = Reader[3].ToString();
                            LengthInt.ID = "WindowLength_" + j.ToString() + WindowOrdinal.ToString();
                            Label Height = new Label();
                            Height.Text = "高";
                            TextBox HeightInt = new TextBox();
                            HeightInt.Text = Reader[2].ToString();
                            HeightInt.ID = "WindowHeight_" + j.ToString() + WindowOrdinal.ToString();
                            P7.Controls.Add(WindowNum);
                            P7.Controls.Add(Length);
                            P7.Controls.Add(LengthInt);
                            P7.Controls.Add(Height);
                            P7.Controls.Add(HeightInt);
                        }
                        Reader.Close();
                        connection.Close();
                    }

                    //查询适合该房间的材料并列出
                    string RTypeID = "RoomType_" + j.ToString();
                    Label RType = (Label)FindControl(RTypeID);
                    Panel P5 = new Panel();
                    PP2.Controls.Add(P5);
                    Panel P8 = new Panel();
                    PP2.Controls.Add(P8);
                    string MatQuery = " SELECT * FROM Material WHERE MatchType='" + RType.Text + "' OR MatchType = '通用'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(MatQuery, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        while (Reader.Read())
                        {
                            //按件计算的材料
                            if (Convert.ToInt32(Reader[3].ToString()) == 0)
                            {
                                OtherOrdinal++;
                                CheckBox CB = new CheckBox();
                                CB.ID = "CB_" + j.ToString() + OtherOrdinal.ToString();
                                Label Matname = new Label();
                                Matname.ID = "Matname_" + j.ToString() + OtherOrdinal.ToString();
                                Matname.Text = Reader[2].ToString();
                                TextBox MatNum = new TextBox();
                                MatNum.ID = "MN_" + j.ToString() + OtherOrdinal.ToString();
                                MatNum.Width = 30;
                                MatNum.Text = "0";
                                Label MaterID = new Label();
                                MaterID.ID = "MatID_" + j.ToString() + OtherOrdinal.ToString();
                                MaterID.Text = Reader[0].ToString();
                                MaterID.Width = 20;
                                MaterID.Height = 20;
                                MaterID.Visible = false;
                                Label ProName = new Label();
                                ProName.ID = "PN_" + j.ToString() + OtherOrdinal.ToString();
                                ProName.Text = Reader[6].ToString();
                                ProName.Width = 20;
                                ProName.Height = 20;
                                ProName.Visible = false;
                                Label unitprice1 = new Label();
                                unitprice1.ID = "UP2_" + j.ToString() + OtherOrdinal.ToString();
                                unitprice1.Text = Reader[4].ToString();
                                unitprice1.Width = 20;
                                unitprice1.Height = 20;
                                unitprice1.Visible = false;
                                P8.Controls.Add(CB);
                                P8.Controls.Add(Matname);
                                P8.Controls.Add(MatNum);
                                P8.Controls.Add(MaterID);
                                P8.Controls.Add(ProName);
                                P8.Controls.Add(unitprice1);
                            }
                            //按面积计算的材料
                            if (Convert.ToInt32(Reader[4].ToString()) == 0)
                            {
                                OtherOrdinalArea++;
                                CheckBox CBA = new CheckBox();
                                CBA.ID = "CBA_" + j.ToString() + OtherOrdinalArea.ToString();
                                DropDownList DropL = new DropDownList();
                                DropL.Items.Add("地面");
                                DropL.Items.Add("墙");
                                DropL.ID = "DropL_" + j.ToString() + OtherOrdinalArea.ToString();
                                Label MatnameArea = new Label();
                                MatnameArea.Text = Reader[2].ToString();
                                MatnameArea.ID = "MA_" + j.ToString() + OtherOrdinalArea.ToString();
                                Label MaterID = new Label();
                                MaterID.ID = "MaterialID_" + j.ToString() + OtherOrdinalArea.ToString();
                                MaterID.Text = Reader[0].ToString();
                                MaterID.Width = 20;
                                MaterID.Height = 20;
                                MaterID.Visible = false;
                                Label ProName = new Label();
                                ProName.ID = "PName_" + j.ToString() + OtherOrdinalArea.ToString();
                                ProName.Text = Reader[6].ToString();
                                ProName.Width = 20;
                                ProName.Height = 20;
                                ProName.Visible = false;
                                Label unitprice2 = new Label();
                                unitprice2.ID = "UP1_" + j.ToString() + OtherOrdinalArea.ToString();
                                unitprice2.Text = Reader[3].ToString();
                                unitprice2.Width = 20;
                                unitprice2.Height = 20;
                                unitprice2.Visible = false;
                                P5.Controls.Add(CBA);
                                P5.Controls.Add(MatnameArea);
                                P5.Controls.Add(DropL);
                                P5.Controls.Add(MaterID);
                                P5.Controls.Add(ProName);
                                P5.Controls.Add(unitprice2);
                            }
                        }
                        Reader.Close();
                        connection.Close();
                    }

                }
                Session["RoomNum"] = num;
                //if (Session["flag"].ToString().Equals("false")) 
                if (Session["flag"] == null)
                {
                    Panel1.Enabled = false;
                    Button1.Visible = false;
                    Button2.Visible = true;
                    Button3.Visible = false;
                    Button4.Visible = true;
                }
                if (Session["flag"]!=null) 
                {
                    Panel1.Enabled = true;
                    Button1.Visible = true;
                    Button2.Visible = false;
                    Button3.Visible = false;
                    Button4.Visible = false;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= Convert.ToInt32(Session["RoomNum"].ToString()); i++)
            {
                double floorarea = 0;
                double wallarea = 0;
                double FloorPrice = 0;
                double WallPrice = 0;

                //更新各个房间信息
                string LID = "Length_" + i.ToString();
                string WID = "Width_" + i.ToString();
                string HID = "Height_" + i.ToString();
                string RID = "RoomID_" + i.ToString();
                string RType = "RoomType_" + i.ToString();
                TextBox TBL = (TextBox)FindControl(LID);
                TextBox TBW = (TextBox)FindControl(WID);
                TextBox TBH = (TextBox)FindControl(HID);
                Label R = (Label)FindControl(RID);
                Label Type = (Label)FindControl(RType);
                string updateroom = "UPDATE RoomInfo SET RoomHeight = '" + TBH.Text + "',RoomLength = '" + TBL.Text + "',RoomWidth='" + TBW.Text + "' WHERE Room_ID = '" + R.Text + "'";
                data.ExcuteSql(updateroom);

                floorarea = (Convert.ToInt32(TBL.Text) * Convert.ToInt32(TBW.Text)) / 10000;
                wallarea = (Convert.ToInt32(TBL.Text) * Convert.ToInt32(TBH.Text) + Convert.ToInt32(TBW.Text) * Convert.ToInt32(TBH.Text)) * 2 / 10000 + floorarea;

                //更新各个门窗信息
                int DoorOrdinal = 1;
                string DL = "DoorLength_" + i.ToString() + DoorOrdinal.ToString();
                string DH = "DoorHeight_" + i.ToString() + DoorOrdinal.ToString();
                while (FindControl(DL) != null)
                {
                    TextBox TBDoorLength = (TextBox)FindControl(DL);
                    TextBox TBDoorHeight = (TextBox)FindControl(DH);
                    string updatedoor = "UPDATE DoorInfo SET DoorHeight = '" + TBDoorHeight.Text + "',DoorLength = '" + TBDoorLength.Text + "' WHERE Room_ID = '" + R.Text + "'";
                    data.ExcuteSql(updatedoor);
                    DoorOrdinal++;
                    DL = "DoorLength_" + i.ToString() + DoorOrdinal.ToString();
                    DH = "DoorHeight_" + i.ToString() + DoorOrdinal.ToString();
                    wallarea -= (Convert.ToInt32(TBDoorHeight.Text) * Convert.ToInt32(TBDoorLength.Text)) / 10000;
                }

                //窗
                int WindowOrdinal = 1;
                string WL = "WindowLength_" + i.ToString() + WindowOrdinal.ToString();
                string WH = "WindowHeight_" + i.ToString() + WindowOrdinal.ToString();
                while (FindControl(WL) != null)
                {
                    TextBox TBWindowLength = (TextBox)FindControl(WL);
                    TextBox TBWindowHeight = (TextBox)FindControl(WH);
                    string updatewindow = "UPDATE WindowInfo SET WindHeight = '" + TBWindowHeight.Text + "',WindLength = '" + TBWindowLength.Text + "' WHERE Room_ID = '" + R.Text + "'";
                    data.ExcuteSql(updatewindow);
                    WindowOrdinal++;
                    WL = "WindowLength_" + i.ToString() + DoorOrdinal.ToString();
                    WH = "WindowHeight_" + i.ToString() + DoorOrdinal.ToString();
                    wallarea -= (Convert.ToInt32(TBWindowHeight.Text) * Convert.ToInt32(TBWindowLength.Text)) / 10000;
                }

                //向FloorInfo中添加信息,在ProjectInfo中添加铺地砖项
                FloorPrice = floorarea * Convert.ToInt32(Label2.Text);
                string InFloor = "INSERT INTO FloorInfo (Room_ID,Price_Worker) VALUES ('" + R.Text + "', '" + FloorPrice.ToString() + "');";
                data.ExcuteSql(InFloor);
                //查询ProJectInfo中是否有这项
                int n1;
                string num1 = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(num1, connection);
                    command.Connection.Open();
                    n1 = Convert.ToInt32(command.ExecuteScalar());
                }

                //没有记录则在ProJectInfo中添加记录
                if (n1 == 0)
                {
                    string InProjectInfo1 = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','铺地砖','" + FloorPrice.ToString() + "','n');";
                    data.ExcuteSql(InProjectInfo1);
                }

                //有记录则更新总价
                double TotalPrice1 = 0;
                if (n1 == 1)
                {
                    string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(getprice, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        Reader.Read();
                        TotalPrice1 = Convert.ToInt32(Reader[0]);
                        Reader.Close();
                        connection.Close();
                    }
                    TotalPrice1 += FloorPrice;
                    string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice1.ToString() + "' WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                    data.ExcuteSql(updateprice);
                }


                //向WallInfo中添加信息，在ProjectInfo中添加粉刷墙漆项
                WallPrice = wallarea * Convert.ToInt32(Label4.Text);
                string InWall = "INSERT INTO WallInfo (Room_ID,Salary_Price) VALUES ('" + R.Text + "', '" + WallPrice.ToString() + "');";
                data.ExcuteSql(InWall);
                //查询ProJectInfo中是否有这项
                int n2;
                string num2 = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(num2, connection);
                    command.Connection.Open();
                    n2 = Convert.ToInt32(command.ExecuteScalar());
                }

                //没有记录则在ProJectInfo中添加记录
                if (n2 == 0)
                {
                    string InProjectInfo2 = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','粉刷墙漆','" + WallPrice.ToString() + "','n');";
                    data.ExcuteSql(InProjectInfo2);
                }

                //有记录则更新总价
                double TotalPrice2 = 0;
                if (n2 == 1)
                {
                    string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(getprice, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        Reader.Read();
                        TotalPrice2 = Convert.ToInt32(Reader[0]);
                        Reader.Close();
                        connection.Close();
                    }
                    TotalPrice2 += WallPrice;
                    string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice2.ToString() + "' WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                    data.ExcuteSql(updateprice);
                }


                //在SelectItem表中按房间添加记录
                int AreaItemOrdinal = 1;
                string C = "CBA_" + i.ToString() + AreaItemOrdinal.ToString();
                string DpL = "DropL_" + i.ToString() + AreaItemOrdinal.ToString();
                string M = "MA_" + i.ToString() + AreaItemOrdinal.ToString();
                string MaterialID1 = "MaterialID_" + i.ToString() + AreaItemOrdinal.ToString();
                string PName = "PName_" + i.ToString() + AreaItemOrdinal.ToString();
                string UP1 = "UP1_" + i.ToString() + AreaItemOrdinal.ToString();
                while (FindControl(C) != null)
                {
                    CheckBox Check = (CheckBox)FindControl(C);
                    Label Mname = (Label)FindControl(M);
                    DropDownList DD = (DropDownList)FindControl(DpL);
                    Label MI = (Label)FindControl(MaterialID1);
                    Label Pname1 = (Label)FindControl(PName);
                    Label unitP1 = (Label)FindControl(UP1);

                    if (Check.Checked == true)
                    {
                        //得到所选材料单价和材料ID和项目名
                        string proName = Pname1.Text;
                        int unitprice = Convert.ToInt32(unitP1.Text);
                        int MaterialID = Convert.ToInt32(MI.Text);

                        //材料总价
                        double price = 0;
                        if (Check.Checked == true && DD.Text.Equals("地面"))
                        {
                            //Price=单价*地板面积
                            price = unitprice * floorarea;
                            string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                            data.ExcuteSql(InSelectItem);

                            //查询ProJectInfo中是否有这项
                            int n;
                            string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(num, connection);
                                command.Connection.Open();
                                n = Convert.ToInt32(command.ExecuteScalar());
                            }

                            //没有记录则在ProJectInfo中添加记录
                            if (n == 0)
                            {
                                string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + Pname1.Text + "','" + price.ToString() + "','n');";
                                data.ExcuteSql(InProjectInfo);
                            }

                            //有记录则更新总价
                            double TotalPrice = 0;
                            if (n == 1)
                            {
                                string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                using (SqlConnection connection = new SqlConnection(strConnection))
                                {
                                    SqlCommand command = new SqlCommand(getprice, connection);
                                    connection.Open();
                                    SqlDataReader Reader = command.ExecuteReader();
                                    Reader.Read();
                                    TotalPrice = Convert.ToInt32(Reader[0]);
                                    Reader.Close();
                                    connection.Close();
                                }
                                TotalPrice += price;
                                string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice.ToString() + "' WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                data.ExcuteSql(updateprice);
                            }
                        }
                        if (Check.Checked == true && DD.Text.Equals("墙"))
                        {
                            //Price=单价*墙面面积
                            price = unitprice * wallarea;
                            string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                            data.ExcuteSql(InSelectItem);

                            //查询ProJectInfo中是否有这项
                            int n;
                            string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(num, connection);
                                command.Connection.Open();
                                n = Convert.ToInt32(command.ExecuteScalar());
                            }

                            //没有记录则在ProJectInfo中添加记录
                            if (n == 0)
                            {
                                string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + Pname1.Text + "','" + price.ToString() + "','n');";
                                data.ExcuteSql(InProjectInfo);
                            }

                            //有记录则更新总价
                            double TotalPrice = 0;
                            if (n == 1)
                            {
                                string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                using (SqlConnection connection = new SqlConnection(strConnection))
                                {
                                    SqlCommand command = new SqlCommand(getprice, connection);
                                    connection.Open();
                                    SqlDataReader Reader = command.ExecuteReader();
                                    Reader.Read();
                                    TotalPrice = Convert.ToInt32(Reader[0]);
                                    Reader.Close();
                                    connection.Close();
                                }
                                TotalPrice += price;
                                string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice.ToString() + "' WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                data.ExcuteSql(updateprice);
                            }
                        }
                    }
                    AreaItemOrdinal++;
                    C = "CBA_" + i.ToString() + AreaItemOrdinal.ToString();
                    DpL = "DropL_" + i.ToString() + AreaItemOrdinal.ToString();
                    M = "MA_" + i.ToString() + AreaItemOrdinal.ToString();
                    MaterialID1 = "MaterialID_" + i.ToString() + AreaItemOrdinal.ToString();
                    PName = "PName_" + i.ToString() + AreaItemOrdinal.ToString();
                    UP1 = "UP1_" + i.ToString() + AreaItemOrdinal.ToString();

                }

                int ItemOrdinal = 1;
                string Check1 = "CB_" + i.ToString() + ItemOrdinal.ToString();
                string Matname1 = "Matname_" + i.ToString() + ItemOrdinal.ToString();
                string MatNum1 = "MN_" + i.ToString() + ItemOrdinal.ToString();
                string MatID1 = "MatID_" + i.ToString() + ItemOrdinal.ToString();
                string ProName1 = "PN_" + i.ToString() + ItemOrdinal.ToString();
                string UP2 = "UP2_" + i.ToString() + ItemOrdinal.ToString();
                while (FindControl(Check1) != null)
                {
                    CheckBox Check2 = (CheckBox)FindControl(Check1);
                    Label Matername = (Label)FindControl(Matname1);
                    TextBox MNum = (TextBox)FindControl(MatNum1);
                    Label MaID = (Label)FindControl(MatID1);
                    Label ProName = (Label)FindControl(ProName1);
                    Label unitP2 = (Label)FindControl(UP2);

                    if (Check2.Checked == true)
                    {
                        //得到所选材料单价和材料ID和项目名
                        int unitprice = Convert.ToInt32(unitP2.Text);
                        int MaterialID = Convert.ToInt32(MaID.Text);
                        string proName = ProName.Text;

                        //在SelectItem中添加一条记录
                        int price = unitprice * Convert.ToInt32(MNum.Text);
                        string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                        data.ExcuteSql(InSelectItem);

                        //查询ProJectInfo中是否有这项
                        int n;
                        string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                        using (SqlConnection connection = new SqlConnection(strConnection))
                        {
                            SqlCommand command = new SqlCommand(num, connection);
                            command.Connection.Open();
                            n = Convert.ToInt32(command.ExecuteScalar());
                        }

                        //没有记录则在ProJectInfo中添加记录
                        if (n == 0)
                        {
                            string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + ProName.Text + "','" + price.ToString() + "','n');";
                            data.ExcuteSql(InProjectInfo);
                        }

                        //有记录则更新总价
                        int TotalPrice11 = 0;
                        if (n == 1)
                        {
                            string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(getprice, connection);
                                connection.Open();
                                SqlDataReader Reader = command.ExecuteReader();
                                Reader.Read();
                                TotalPrice11 = Convert.ToInt32(Reader[0]);
                                Reader.Close();
                                connection.Close();
                            }
                            TotalPrice11 += price;
                            string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice11.ToString() + "' WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            data.ExcuteSql(updateprice);
                        }
                    }

                    ItemOrdinal++;
                    Check1 = "CB_" + i.ToString() + ItemOrdinal.ToString();
                    Matname1 = "Matname_" + i.ToString() + ItemOrdinal.ToString();
                    MatNum1 = "MN_" + i.ToString() + ItemOrdinal.ToString();
                    MatID1 = "MatID_" + i.ToString() + ItemOrdinal.ToString();
                    ProName1 = "PN_" + i.ToString() + ItemOrdinal.ToString();
                    UP2 = "UP2_" + i.ToString() + ItemOrdinal.ToString();
                }

            }
            Session["flag"] = "true";
            Response.Redirect("matBudget.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel1.Enabled = true;
            Button3.Visible = true;
            Button2.Visible = false;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Session["flag"] = "true";
            Response.Redirect("matBudget.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //删除旧信息
            string delSelectItem = " delete from SelectItem where Room_ID in(Select Room_ID from RoomInfo where House_ID ='" + Session["House_ID"].ToString() + "')";
            data.ExcuteSql(delSelectItem);
            string delProjectInfo = "delete from ProjectInfo where House_ID='" + Session["House_ID"].ToString() + "'";
            data.ExcuteSql(delProjectInfo);

            for (int i = 1; i <= Convert.ToInt32(Session["RoomNum"].ToString()); i++)
            {
                double floorarea = 0;
                double wallarea = 0;
                double FloorPrice = 0;
                double WallPrice = 0;

                //更新各个房间信息
                string LID = "Length_" + i.ToString();
                string WID = "Width_" + i.ToString();
                string HID = "Height_" + i.ToString();
                string RID = "RoomID_" + i.ToString();
                string RType = "RoomType_" + i.ToString();
                TextBox TBL = (TextBox)FindControl(LID);
                TextBox TBW = (TextBox)FindControl(WID);
                TextBox TBH = (TextBox)FindControl(HID);
                Label R = (Label)FindControl(RID);
                Label Type = (Label)FindControl(RType);
                string updateroom = "UPDATE RoomInfo SET RoomHeight = '" + TBH.Text + "',RoomLength = '" + TBL.Text + "',RoomWidth='" + TBW.Text + "' WHERE Room_ID = '" + R.Text + "'";
                data.ExcuteSql(updateroom);

                floorarea = (Convert.ToInt32(TBL.Text) * Convert.ToInt32(TBW.Text)) / 10000;
                wallarea = (Convert.ToInt32(TBL.Text) * Convert.ToInt32(TBH.Text) + Convert.ToInt32(TBW.Text) * Convert.ToInt32(TBH.Text)) * 2 / 10000 + floorarea;

                //更新各个门窗信息
                int DoorOrdinal = 1;
                string DL = "DoorLength_" + i.ToString() + DoorOrdinal.ToString();
                string DH = "DoorHeight_" + i.ToString() + DoorOrdinal.ToString();
                while (FindControl(DL) != null)
                {
                    TextBox TBDoorLength = (TextBox)FindControl(DL);
                    TextBox TBDoorHeight = (TextBox)FindControl(DH);
                    string updatedoor = "UPDATE DoorInfo SET DoorHeight = '" + TBDoorHeight.Text + "',DoorLength = '" + TBDoorLength.Text + "' WHERE Room_ID = '" + R.Text + "'";
                    data.ExcuteSql(updatedoor);
                    DoorOrdinal++;
                    DL = "DoorLength_" + i.ToString() + DoorOrdinal.ToString();
                    DH = "DoorHeight_" + i.ToString() + DoorOrdinal.ToString();
                    wallarea -= (Convert.ToInt32(TBDoorHeight.Text) * Convert.ToInt32(TBDoorLength.Text)) / 10000;
                }

                //窗
                int WindowOrdinal = 1;
                string WL = "WindowLength_" + i.ToString() + WindowOrdinal.ToString();
                string WH = "WindowHeight_" + i.ToString() + WindowOrdinal.ToString();
                while (FindControl(WL) != null)
                {
                    TextBox TBWindowLength = (TextBox)FindControl(WL);
                    TextBox TBWindowHeight = (TextBox)FindControl(WH);
                    string updatewindow = "UPDATE WindowInfo SET WindHeight = '" + TBWindowHeight.Text + "',WindLength = '" + TBWindowLength.Text + "' WHERE Room_ID = '" + R.Text + "'";
                    data.ExcuteSql(updatewindow);
                    WindowOrdinal++;
                    WL = "WindowLength_" + i.ToString() + DoorOrdinal.ToString();
                    WH = "WindowHeight_" + i.ToString() + DoorOrdinal.ToString();
                    wallarea -= (Convert.ToInt32(TBWindowHeight.Text) * Convert.ToInt32(TBWindowLength.Text)) / 10000;
                }

                //向FloorInfo中添加信息,在ProjectInfo中添加铺地砖项
                FloorPrice = floorarea * Convert.ToInt32(Label2.Text);
                string InFloor = "INSERT INTO FloorInfo (Room_ID,Price_Worker) VALUES ('" + R.Text + "', '" + FloorPrice.ToString() + "');";
                data.ExcuteSql(InFloor);
                //查询ProJectInfo中是否有这项
                int n1;
                string num1 = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(num1, connection);
                    command.Connection.Open();
                    n1 = Convert.ToInt32(command.ExecuteScalar());
                }

                //没有记录则在ProJectInfo中添加记录
                if (n1 == 0)
                {
                    string InProjectInfo1 = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','铺地砖','" + FloorPrice.ToString() + "','n');";
                    data.ExcuteSql(InProjectInfo1);
                }

                //有记录则更新总价
                double TotalPrice1 = 0;
                if (n1 == 1)
                {
                    string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(getprice, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        Reader.Read();
                        TotalPrice1 = Convert.ToInt32(Reader[0]);
                        Reader.Close();
                        connection.Close();
                    }
                    TotalPrice1 += FloorPrice;
                    string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice1.ToString() + "' WHERE ProjectName='铺地砖' and House_ID='" + Session["House_ID"].ToString() + "'";
                    data.ExcuteSql(updateprice);
                }


                //向WallInfo中添加信息，在ProjectInfo中添加粉刷墙漆项
                WallPrice = wallarea * Convert.ToInt32(Label4.Text);
                string InWall = "INSERT INTO WallInfo (Room_ID,Salary_Price) VALUES ('" + R.Text + "', '" + WallPrice.ToString() + "');";
                data.ExcuteSql(InWall);
                //查询ProJectInfo中是否有这项
                int n2;
                string num2 = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(num2, connection);
                    command.Connection.Open();
                    n2 = Convert.ToInt32(command.ExecuteScalar());
                }

                //没有记录则在ProJectInfo中添加记录
                if (n2 == 0)
                {
                    string InProjectInfo2 = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','粉刷墙漆','" + WallPrice.ToString() + "','n');";
                    data.ExcuteSql(InProjectInfo2);
                }

                //有记录则更新总价
                double TotalPrice2 = 0;
                if (n2 == 1)
                {
                    string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                    using (SqlConnection connection = new SqlConnection(strConnection))
                    {
                        SqlCommand command = new SqlCommand(getprice, connection);
                        connection.Open();
                        SqlDataReader Reader = command.ExecuteReader();
                        Reader.Read();
                        TotalPrice2 = Convert.ToInt32(Reader[0]);
                        Reader.Close();
                        connection.Close();
                    }
                    TotalPrice2 += WallPrice;
                    string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice2.ToString() + "' WHERE ProjectName='粉刷墙漆' and House_ID='" + Session["House_ID"].ToString() + "'";
                    data.ExcuteSql(updateprice);
                }


                //在SelectItem表中按房间添加记录
                int AreaItemOrdinal = 1;
                string C = "CBA_" + i.ToString() + AreaItemOrdinal.ToString();
                string DpL = "DropL_" + i.ToString() + AreaItemOrdinal.ToString();
                string M = "MA_" + i.ToString() + AreaItemOrdinal.ToString();
                string MaterialID1 = "MaterialID_" + i.ToString() + AreaItemOrdinal.ToString();
                string PName = "PName_" + i.ToString() + AreaItemOrdinal.ToString();
                string UP1 = "UP1_" + i.ToString() + AreaItemOrdinal.ToString();
                while (FindControl(C) != null)
                {
                    CheckBox Check = (CheckBox)FindControl(C);
                    Label Mname = (Label)FindControl(M);
                    DropDownList DD = (DropDownList)FindControl(DpL);
                    Label MI = (Label)FindControl(MaterialID1);
                    Label Pname1 = (Label)FindControl(PName);
                    Label unitP1 = (Label)FindControl(UP1);

                    if (Check.Checked == true)
                    {
                        //得到所选材料单价和材料ID和项目名
                        string proName = Pname1.Text;
                        int unitprice = Convert.ToInt32(unitP1.Text);
                        int MaterialID = Convert.ToInt32(MI.Text);

                        //材料总价
                        double price = 0;
                        if (Check.Checked == true && DD.Text.Equals("地面"))
                        {
                            //Price=单价*地板面积
                            price = unitprice * floorarea;
                            string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                            data.ExcuteSql(InSelectItem);

                            //查询ProJectInfo中是否有这项
                            int n;
                            string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(num, connection);
                                command.Connection.Open();
                                n = Convert.ToInt32(command.ExecuteScalar());
                            }

                            //没有记录则在ProJectInfo中添加记录
                            if (n == 0)
                            {
                                string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + Pname1.Text + "','" + price.ToString() + "','n');";
                                data.ExcuteSql(InProjectInfo);
                            }

                            //有记录则更新总价
                            double TotalPrice = 0;
                            if (n == 1)
                            {
                                string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                using (SqlConnection connection = new SqlConnection(strConnection))
                                {
                                    SqlCommand command = new SqlCommand(getprice, connection);
                                    connection.Open();
                                    SqlDataReader Reader = command.ExecuteReader();
                                    Reader.Read();
                                    TotalPrice = Convert.ToInt32(Reader[0]);
                                    Reader.Close();
                                    connection.Close();
                                }
                                TotalPrice += price;
                                string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice.ToString() + "' WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                data.ExcuteSql(updateprice);
                            }
                        }
                        if (Check.Checked == true && DD.Text.Equals("墙"))
                        {
                            //Price=单价*墙面面积
                            price = unitprice * wallarea;
                            string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                            data.ExcuteSql(InSelectItem);

                            //查询ProJectInfo中是否有这项
                            int n;
                            string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(num, connection);
                                command.Connection.Open();
                                n = Convert.ToInt32(command.ExecuteScalar());
                            }

                            //没有记录则在ProJectInfo中添加记录
                            if (n == 0)
                            {
                                string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + Pname1.Text + "','" + price.ToString() + "','n');";
                                data.ExcuteSql(InProjectInfo);
                            }

                            //有记录则更新总价
                            double TotalPrice = 0;
                            if (n == 1)
                            {
                                string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                using (SqlConnection connection = new SqlConnection(strConnection))
                                {
                                    SqlCommand command = new SqlCommand(getprice, connection);
                                    connection.Open();
                                    SqlDataReader Reader = command.ExecuteReader();
                                    Reader.Read();
                                    TotalPrice = Convert.ToInt32(Reader[0]);
                                    Reader.Close();
                                    connection.Close();
                                }
                                TotalPrice += price;
                                string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice.ToString() + "' WHERE ProjectName='" + Pname1.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                                data.ExcuteSql(updateprice);
                            }
                        }
                    }
                    AreaItemOrdinal++;
                    C = "CBA_" + i.ToString() + AreaItemOrdinal.ToString();
                    DpL = "DropL_" + i.ToString() + AreaItemOrdinal.ToString();
                    M = "MA_" + i.ToString() + AreaItemOrdinal.ToString();
                    MaterialID1 = "MaterialID_" + i.ToString() + AreaItemOrdinal.ToString();
                    PName = "PName_" + i.ToString() + AreaItemOrdinal.ToString();
                    UP1 = "UP1_" + i.ToString() + AreaItemOrdinal.ToString();

                }

                int ItemOrdinal = 1;
                string Check1 = "CB_" + i.ToString() + ItemOrdinal.ToString();
                string Matname1 = "Matname_" + i.ToString() + ItemOrdinal.ToString();
                string MatNum1 = "MN_" + i.ToString() + ItemOrdinal.ToString();
                string MatID1 = "MatID_" + i.ToString() + ItemOrdinal.ToString();
                string ProName1 = "PN_" + i.ToString() + ItemOrdinal.ToString();
                string UP2 = "UP2_" + i.ToString() + ItemOrdinal.ToString();
                while (FindControl(Check1) != null)
                {
                    CheckBox Check2 = (CheckBox)FindControl(Check1);
                    Label Matername = (Label)FindControl(Matname1);
                    TextBox MNum = (TextBox)FindControl(MatNum1);
                    Label MaID = (Label)FindControl(MatID1);
                    Label ProName = (Label)FindControl(ProName1);
                    Label unitP2 = (Label)FindControl(UP2);

                    if (Check2.Checked == true)
                    {
                        //得到所选材料单价和材料ID和项目名
                        int unitprice = Convert.ToInt32(unitP2.Text);
                        int MaterialID = Convert.ToInt32(MaID.Text);
                        string proName = ProName.Text;

                        //在SelectItem中添加一条记录
                        int price = unitprice * Convert.ToInt32(MNum.Text);
                        string InSelectItem = "INSERT INTO SelectItem (Room_ID,Mat_ID,Price,Unit_Price) VALUES ('" + R.Text + "','" + MaterialID.ToString() + "','" + price.ToString() + "', '" + unitprice.ToString() + "');";
                        data.ExcuteSql(InSelectItem);

                        //查询ProJectInfo中是否有这项
                        int n;
                        string num = "SELECT count(Pro_ID) FROM ProjectInfo WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                        using (SqlConnection connection = new SqlConnection(strConnection))
                        {
                            SqlCommand command = new SqlCommand(num, connection);
                            command.Connection.Open();
                            n = Convert.ToInt32(command.ExecuteScalar());
                        }

                        //没有记录则在ProJectInfo中添加记录
                        if (n == 0)
                        {
                            string InProjectInfo = "INSERT INTO ProjectInfo (House_ID,ProjectName,TotalPrice,State) VALUES ('" + Session["House_ID"].ToString() + "','" + ProName.Text + "','" + price.ToString() + "','n');";
                            data.ExcuteSql(InProjectInfo);
                        }

                        //有记录则更新总价
                        int TotalPrice11 = 0;
                        if (n == 1)
                        {
                            string getprice = "SELECT TotalPrice FROM ProjectInfo WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            using (SqlConnection connection = new SqlConnection(strConnection))
                            {
                                SqlCommand command = new SqlCommand(getprice, connection);
                                connection.Open();
                                SqlDataReader Reader = command.ExecuteReader();
                                Reader.Read();
                                TotalPrice11 = Convert.ToInt32(Reader[0]);
                                Reader.Close();
                                connection.Close();
                            }
                            TotalPrice11 += price;
                            string updateprice = "UPDATE ProjectInfo SET TotalPrice = '" + TotalPrice11.ToString() + "' WHERE ProjectName='" + ProName.Text + "' and House_ID='" + Session["House_ID"].ToString() + "'";
                            data.ExcuteSql(updateprice);
                        }
                    }

                    ItemOrdinal++;
                    Check1 = "CB_" + i.ToString() + ItemOrdinal.ToString();
                    Matname1 = "Matname_" + i.ToString() + ItemOrdinal.ToString();
                    MatNum1 = "MN_" + i.ToString() + ItemOrdinal.ToString();
                    MatID1 = "MatID_" + i.ToString() + ItemOrdinal.ToString();
                    ProName1 = "PN_" + i.ToString() + ItemOrdinal.ToString();
                    UP2 = "UP2_" + i.ToString() + ItemOrdinal.ToString();
                }

            }
            Button3.Visible = false;
        }

        protected void LogoutLinkButton_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();  //登出页面并释放所有session
            Session.Abandon();
            Response.Redirect("../logon.aspx");
        }

    }
}
